﻿//
// © Algobox Ltd - Benn Eichhorn
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Algobox.Objects.Finance.Assets.Stock;
using Algobox.Structure.Finance.Strategies.SmartStoploss;

namespace Algobox.Structure.Finance.Strategies.SpikeArb
{
    public class SpikeArbFill : INotifyPropertyChanged
    {
        #region Constructors

        public SpikeArbFill(SpikeArbLegListing collar, SmartStoplossOrder orderExit, bool isBuy, double collarPercentage, double exitPercentage, uint quantity, double collarPrice, double targetPrice)
        {
            _collar = collar;
            _orderExit = orderExit;
            _isBuy = isBuy;
            _collarPercentage = isBuy ? -collarPercentage : collarPercentage;
            _exitPercentage = exitPercentage;
            _quantity = quantity;
            _collarPrice = collarPrice;
            _targetPrice = targetPrice;
            _stopPrice = orderExit.PriceStop;

            if (_collar.Stock.Security.Currency == Objects.Finance.Currencies.Currency.GBX)
                _divisor = 100;
            else
                _divisor = 1;
        }

        #endregion
        //////////////////
        #region Member Variables

        SpikeArbLegListing _collar;
        SmartStoplossOrder _orderExit;
        bool _isBuy;
        double _collarPercentage;
        double _exitPercentage;
        uint _quantity;
        uint _quantityRemaining;
        double _collarPrice;
        double _targetPrice;
        double _stopPrice;
        double _realisedPrice;
        int _divisor;

        #endregion
        //////////////////
        #region Properties

        public string Stock { get { return (_collar == null) ? "Unknown" : _collar.Stock.ToString(); } }
        public string Direction { get { return _isBuy ? "Buy" : "Sell"; } }
        public string Collar { get { return (_isBuy ? "-" : "") + _collarPercentage + "%"; } }
        public string Exit { get { return (_collarPercentage - (_collarPercentage * _exitPercentage)) + "%"; } }
        public double Price { get { return _collarPrice; } }
        public uint Quantity { get { return _quantity; } }
        public uint Remaining { get { return _quantityRemaining; } internal set { _quantityRemaining = value; NotifyPropertyChanged("Remaining"); } }
        public double Target { get { return _targetPrice; } set { _targetPrice = value; _orderExit.PriceTarget = _targetPrice; NotifyPropertyChanged("Target"); } }
        public double TargetPL { get { return CalculateProfit(_targetPrice); } }
        public double Stop { get { return _stopPrice; } set { _stopPrice = value; _orderExit.PriceStop = _stopPrice; NotifyPropertyChanged("Stop"); } }
        public double StopPL { get { return CalculateProfit(_stopPrice); } }
        public double Realised { get { return _realisedPrice; } set { _realisedPrice = value; NotifyPropertyChanged("Realised"); NotifyPropertyChanged("RealisedPL"); NotifyPropertyChanged("Slippage"); } }
        public double RealisedPL { get { return CalculateProfit(_realisedPrice); } }
        public double Slippage { get { if (_realisedPrice == 0) return 0; return RealisedPL - TargetPL; } }

        #endregion
        //////////////////
        #region Functions

        private double CalculateProfit(double exitPrice)
        {
            return _isBuy ?
                System.Math.Round((exitPrice - _collarPrice) * _quantity / _divisor, 2) :
                System.Math.Round((_collarPrice - exitPrice) * _quantity / _divisor, 2);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
        //////////////////
        #region Enums



        #endregion
    }
}
